﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using PictureProcessing.Comm;
using System.Threading;

namespace PictureProcessing
{
    public partial class FrmDataSorting : Form
    {
       
        public FrmDataSorting()
        {
            InitializeComponent();
        }

        private void btnSelectDirectory_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog path = new FolderBrowserDialog();
            path.ShowDialog();
            this.txtSelectDirectory.Text = path.SelectedPath;
        }

        private void btnSaveDirectory_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog path = new FolderBrowserDialog();
            path.ShowDialog();
            this.txtSaveDirectory.Text = path.SelectedPath;
        }

        private void btnSignOut_Click(object sender, EventArgs e)
        {
            this.Close();
        }

       
       
        private void btn_do_Click(object sender, EventArgs e)
        {
            try
            {
                string strRoute = txtSelectDirectory.Text;//选择文件目录
                string strSaveRoute = txtSaveDirectory.Text;//保存目录
                string strSaveRoutenew = "";
                string time = DateTime.Now.ToShortDateString().ToString().Replace("-", "").Replace("/", "");

                //创建保存文件夹
                strSaveRoutenew = DirFile.CreateDir(strSaveRoute, time);


                if (string.IsNullOrEmpty(strSaveRoute) || strSaveRoute == " ")
                {
                    MessageBox.Show("选择数据保存路径!");
                    return;
                }

                if (string.IsNullOrEmpty(strRoute))
                {
                    MessageBox.Show("请选择需要分拣的目录!");
                    return;
                }
                else
                {
                    this.backgroundWorker1.RunWorkerAsync(); // 运行 backgroundWorker 组件
                    ProcessForm form = new ProcessForm(this.backgroundWorker1);// 显示进度条窗体
                    form.ShowDialog(this);
                   
                    int strlen = strRoute.Length + 1;
                    string imgtype = "*.BMP|*.JPG|*.GIF|*.PNG";
                    string[] ImageType = imgtype.Split('|');
                    for (int i = 0; i < ImageType.Length; i++)
                    {
                        string[] dirs = Directory.GetFiles(strRoute, ImageType[i]);

                        int j = 0;
                        foreach (string dir in dirs)
                        {
                            //全名路径包含后缀
                            string fullname = dir.Substring(strlen, dir.Length - strlen);
                            //全名路径不包含后缀
                            string rename = fullname.Remove(fullname.LastIndexOf("."));
                            j++;
                            DirFile.Sorting(strSaveRoutenew, fullname, dir);

                        }
                    }
                    
                }
                MessageBox.Show("数据分拣完成!");
                
            }
            catch (Exception ex)
            {

                MessageBox.Show("数据分拣出错:" + ex.Message);
            }




        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;
            for (int i = 0; i < 100; i++)
            {
                Thread.Sleep(100);
                worker.ReportProgress(i);
                if (worker.CancellationPending)  // 如果用户取消则跳出处理数据代码 
                {
                    e.Cancel = true;
                    break;
                }
            }
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                MessageBox.Show(e.Error.Message);
            }
            else if (e.Cancelled)
            {
            }
            else
            {
            }
        }
    }
}
